#!/usr/bin/env python3
#-*- coding:utf-8 -*-
import re,math;
import pymysql;
import datetime;
from util import mysql_util
'''
输入输出
'''
# name = input("input name:\n")
# # name = "haha"
# print(name)
'''
 数据类型:
容器数据类型：str、list、tuple、dict、set；
'''

# list
l = ["abcd",32,None,True]
print(l)
l1 = list(range(1,10))
print(l1)
l1.append("yll")
print(l1)
l1.insert(1,"yll")
print(l1)
l1[2]="aa"
print(l1)
l1.pop()
print(l1)
l1.remove(4)
print(l1)

# tuple
t=("aaaa",None,True,33,["xixixi","hahaha"])
print(t)
t[4][0]="vvvvvv"
print(t)

# dict
d={"age":22,"sex":"nv"}
print(d)
d["age"]=21
print(d)
# 设置一个不存在的就会新添加一个
d["name"]="yll"
print(d)

# set去重无序pop删除拍完序的第一位
s=set(["yll","yll",18,20,22])
print(s)
s.add("lly")
print(s)

# 判断语句
a=22
if a<10:
    print("child")
elif a>=10 and a<23:
    print("青少年")
elif a>23:
    print("老了")
else:
    print("error!")

# 循环语句
for i in l:
    print(i)
l=list("http://www.hao.com/page_{0}.html".format(i) for i in range(1,10))
print(l)

# 函数
def function_1():
    print("This is function")
function_1()
def function_2():
    print(int(12.5))

'''
文件操作 可用with代替try except
'''
def file_opration():
    '''
    try:
        f=open("D:\\aaa.txt",encoding="UTF-8")
        print(f.readlines())
    except Exception as e:
        print(e)
        raise Exception("出错了")
    finally:
        f.close()
    '''
    with open("D:\\aaa.txt","a",encoding="UTF-8") as f:
        f.write("杨丽丽\n")
        f.write("自信点\n")
'''
-----数据库操作-----
首先要导入第三方库pymysql
'''
# mysql—insert插入操作
def mysql_insert():
    # 获取连接对象、游标对象
    connection = pymysql.connect(host='localhost', user='root', passwd='123456', db='myjob', charset='utf8');
    cursor = connection.cursor();
    #进行数据库操作
    insert_user="insert into user(adress,name,password,sex,tel,user_name)" \
                " values('贵州','归子','1998','女','123','guizi')"
    cursor.execute(insert_user)
    #事务提交
    connection.commit()
    cursor.close()

# 数据库查询操作
def mysql_query():
    # 获取连接对象、游标对象
    connection = pymysql.connect(host='localhost', user='root', passwd='123456', db='myjob', charset='utf8');
    cursor = connection.cursor();
    #进行数据库操作
    query_user="select * from user"
    cursor.execute(query_user)
    result=cursor.fetchall()
    print(result)
    #关闭连接和游标
    connection.close()
    cursor.close()

'''
调用util
'''
def mysql_operation(sql):
    result = mysql_util.execute_(sql)
    print(result)

# 第二个方式
if __name__ == "__main__":
    # function_1()
    # function_2()
    # file_opration()
    # mysql_insert()
    # mysql_query()
    insert_user = "insert into user(adress,name,password,sex,tel,user_name)" \
                  " values('织金','丽丽','1998','女','2222','lili')"
    update_user="update user set name='happy' where user_id=5"
    query_user="select * from user"
    mysql_operation(update_user)
